// 编辑文章页面服务逻辑

import type { Article, EditArticleForm } from './types'

const API_BASE_URL = 'http://localhost:3000/api'

// 获取文章详情
export const fetchArticle = async (id: string): Promise<Article> => {
  const token = localStorage.getItem('token')
  const res = await fetch(`${API_BASE_URL}/articles/${id}`, {
    headers: token ? { Authorization: `Bearer ${token}` } : {}
  })

  if (!res.ok) {
    throw new Error('文章不存在')
  }

  return await res.json()
}

// 更新文章
export const updateArticle = async (id: string, form: EditArticleForm): Promise<void> => {
  const token = localStorage.getItem('token')
  const res = await fetch(`${API_BASE_URL}/articles/${id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`
    },
    body: JSON.stringify(form)
  })

  if (!res.ok) {
    const err = await res.json()
    throw new Error(err.message || '更新失败')
  }
} 